﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User_LetterDetail : System.Web.UI.Page
{
    UsersRepository URepo = new UsersRepository();
    UserMailbox MailRepo = new UserMailbox();
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        if (Request.Cookies["userinfo"] != null)
        {
            if (_user["username"] != null)
            {
                int UserId = URepo.GetUserIdByName(_user["username"]);
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        try
                        {
                            int Id = int.Parse(Request.QueryString["ID"]);
                            var mailObj = MailRepo.GetEmailById(UserId, Id);
                            if (mailObj != null)
                            {
                                lblSubject.Text = mailObj.MailBox_Subject;
                                lblFrom.Text = mailObj.User.Users_Email;
                                lblReceiveDate.Text = Convert.ToDateTime(mailObj.MailBox_ReceiveDate).ToShortDateString();
                                ltrContent.Text = mailObj.MailBox_Content;
                                if (mailObj.MailBox_Status == (short)Mailbox.Outbox)
                                    btnReply.Visible = false;
                            }
                            else
                                Response.Redirect(ResolveUrl("~/Mailbox.html"));
                        }
                        catch
                        {
                        }
                    }
                }
            }
            else
                Response.Redirect(ResolveUrl("~/Index.html"));
        }
        else
            Response.Redirect(ResolveUrl("~/Index.html"));
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        int Id = int.Parse(Request.QueryString["ID"]);
        int UserId = URepo.GetUserIdByName(_user["username"]);
        if (MailRepo.DeteteMailbox(UserId, Id))
        {
            Response.Redirect(ResolveUrl("~/Mailbox.html"));
        }
    }

    public static string ShowAlertDeleteMail()
    {
        return "Tin nhắn đã được xóa";
    }
    public string ReplyMsg()
    {
        int MailId = int.Parse(Request.QueryString["ID"]);
        return ResolveUrl("~/Tra-loi/" + MailId + ".html");
    }
    protected void GotoReplyPage(object sender, EventArgs e)
    {
        int MailId = int.Parse(Request.QueryString["ID"]);
        Response.Redirect(ResolveUrl("~/Tra-loi/" + MailId + ".html"));
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl("~/Mailbox.html"));
    }
}